package com.dcjt.firehotcarpad.retrofit1.repository;

import com.aries.library.fast.manager.LoggerManager;
import com.aries.library.fast.retrofit.FastRetrofit;
import com.aries.library.fast.retrofit.FastTransformer;
import com.aries.library.fast.util.SPUtil;
import com.dcjt.firehotcarpad.base.ApiPath;
import com.dcjt.firehotcarpad.base.App;
import com.dcjt.firehotcarpad.base.SPConstant;
import com.dcjt.firehotcarpad.entity.AroundWaterEntity;
import com.dcjt.firehotcarpad.entity.BaseEntity;
import com.dcjt.firehotcarpad.entity.BaseListEntity;
import com.dcjt.firehotcarpad.entity.BiaohuiEntity;
import com.dcjt.firehotcarpad.entity.CarCurrentDetailEntity;
import com.dcjt.firehotcarpad.entity.CarDetailEntity;
import com.dcjt.firehotcarpad.entity.CarZZXX;
import com.dcjt.firehotcarpad.entity.CheckLogin;
import com.dcjt.firehotcarpad.entity.CjXx;
import com.dcjt.firehotcarpad.entity.CombatTeamEntity;
import com.dcjt.firehotcarpad.entity.DictEntity;
import com.dcjt.firehotcarpad.entity.DisasterInfoEntity;
import com.dcjt.firehotcarpad.entity.InstructionListEntity;
import com.dcjt.firehotcarpad.entity.JigouDetailEntity;
import com.dcjt.firehotcarpad.entity.LoginResultEntity;
import com.dcjt.firehotcarpad.entity.SetCLZTBean;
import com.dcjt.firehotcarpad.entity.Template;
import com.dcjt.firehotcarpad.entity.VoiceFileEntity;
import com.dcjt.firehotcarpad.fragment.DisasterListInfoEntity;
import com.dcjt.firehotcarpad.fragment.WaterSourceEntity;
import com.dcjt.firehotcarpad.manager.DataManager;
import com.dcjt.firehotcarpad.retrofit1.service.ApiServices;
import com.google.gson.Gson;
import com.juntu.loginlib.contact.LoginConstant;

import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;


/**
 * Created: AriesHoo on 2017/11/1 17:47
 * E-Mail: AriesHoo@126.com
 * Function: 接口调用
 * Description:
 */
public class ApiRepository1 extends BaseRepository1 {
    private static final String TAG = "ApiRepository1";
    private static volatile ApiRepository1 instance;

    private ApiRepository1() {
    }

    public static ApiRepository1 getInstance() {
        if (instance == null) {
            synchronized (ApiRepository1.class) {
                if (instance == null) {
                    instance = new ApiRepository1();
                }
            }
        }
        return instance;
    }

//    private UserService getUserService() {
//        return FastRetrofit.getInstance().createService(UserService.class);
//    }

    private ApiServices getApiService() {
        return FastRetrofit.getInstance().createService(ApiServices.class);
    }

    /**
     * 登录
     *
     * @param account
     * @param password
     * @return
     */
    public Observable<LoginResultEntity> Login(String account, String password, String cslxdm) {
        String dlsblx = (String) SPUtil.get(App.get(), SPConstant.DLSBLX, "");
        return transformer(getApiService().login(account, password, dlsblx, cslxdm));
    }

    /**
     * 获取基础配置信息
     *
     * @param configType
     * @return
     */
    public Observable<Object> getConfigData(String configType) {
        return getApiService().getConfigData(configType).compose(FastTransformer.switchSchedulers());
    }

    /**
     * 获取基础配置信息
     *
     * @param configType
     * @return
     */
    public Observable<Object> getConfigDataJCJ(String configType) {
        return getApiService().getConfigDataJCJ(configType).compose(FastTransformer.switchSchedulers());
    }

    /**
     * 获取基础配置信息
     *
     * @param configType
     * @return
     */
    public Observable<Object> getConfigDataJCJ_APP(String configType) {
        return getApiService().getConfigDataJCJ_APP(configType).compose(FastTransformer.switchSchedulers());
    }

    /**
     * 发送指令消息
     *
     * @param param
     * @return
     */
    public Observable<ResponseBody> sendMessage_zl(HashMap<String, Object> param, String url) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                new Gson().toJson(param));
        return getApiService().sendMessage_zl(url, body).compose(FastTransformer.switchSchedulers());
    }

    /**
     * 发送指令消息
     *
     * @param param
     * @return
     */
    public Observable<ResponseBody> takeMessage(HashMap<String, Object> param, String url) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                new Gson().toJson(param));
        return getApiService().takeMessage(url, body).compose(FastTransformer.switchSchedulers());
    }

    /**
     * 添加群
     *
     * @return
     */
    public Observable<ResponseBody> takeMessageGET(String aid, String access_token, String uid,
                                                   String size, String confirm, String imei, String url) {

        return getApiService().takeMessageGET(url, aid, access_token, uid, size, confirm, imei).compose(FastTransformer.switchSchedulers());
    }

    /**
     * 添加群
     *
     * @return
     */
    public Observable<ResponseBody> addGroup(String aid, String access_token, String gid,
                                             String id, String url) {

        return getApiService().addGroup(url, aid, access_token, gid, id).compose(FastTransformer.switchSchedulers());
    }

    /**
     * 获取消息服务配置信息
     *
     * @param uid
     * @param pwd
     * @return
     */
    public Observable<ResponseBody> getConfigMessage(String uid, String pwd, String url) {
        return getApiService().getConfigMessage(url, uid, pwd).compose(FastTransformer.switchSchedulers());
    }

    /**
     * 注册消息服务配置信息
     *
     * @param uid
     * @param pwd
     * @return
     */
    public Observable<ResponseBody> getConfigMessageRegister(String uid, String pwd, String appName, String url) {
        return getApiService().getConfigMessageRegister(url, uid, pwd, appName).compose(FastTransformer.switchSchedulers());
    }

    /**
     * 字典表
     *
     * @param code
     * @return
     */
    public Observable<List<DictEntity>> getDictList(String code) {
        return transformer(getApiService().getDictList(code, DataManager.getInstance().getUser().getToken()));
    }

    /**
     * 机构列表
     *
     * @param jgid
     * @return
     */
    public Observable<List<JigouDetailEntity>> getJigouList(String jgid) {
        return transformer(getApiService().getJigouList(jgid));
    }

    /**
     * 车辆列表
     *
     * @param jgid
     * @return
     */
    public Observable<List<CarDetailEntity>> getCarList(String jgid) {
        return transformer(getApiService().getCarList(jgid));
    }

    /**
     * 修改场所类型
     *
     * @param code
     * @return
     */
    public Observable<Object> updateCSLX(String code) {
        return transformer(getApiService().updateCSLX(DataManager.getInstance().getUser().getId(), code, DataManager.getInstance().getUser().getToken()));
    }

    /**
     * 获取录音文件
     *
     * @param zqid
     * @return
     */
    public Observable<VoiceFileEntity> getVoiceFile(String zqid) {
        return transformer(getApiService().getVoiceFile(zqid, DataManager.getInstance().getUser().getToken()));
    }

    /**
     * 获取车辆作战信息
     *
     * @param zqid
     * @return
     */
    public Observable<CarZZXX> getCarZuozhanXX(String zqid) {
        return transformer(getApiService().getCarZuozhanXX(zqid, DataManager.getInstance().getUser().getAccount(), DataManager.getInstance().getUser().getToken()));
    }

    /**
     * 获取车辆信息（titlebar  使用）
     *
     * @return
     */
    public Observable<CarCurrentDetailEntity> getCarStateDetail() {
        return transformer(getApiService().getCarStateDetail(DataManager.getInstance().getUser().getAccount(), DataManager.getInstance().getUser().getToken()));
    }

    /**
     * 请求列表
     *
     * @param param
     * @return
     */
    public Observable<BaseListEntity<DisasterInfoEntity>> getDisasterList(HashMap<String, Object> param) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                new Gson().toJson(param));
        LoggerManager.e(new Gson().toJson(param));
        return transformer(getApiService().getDisasterList(body, DataManager.getInstance().getUser().getToken()));
    }

    /**
     * 获取智能推荐列表
     *
     * @param object
     * @return
     */
    public Observable<BaseListEntity<DisasterInfoEntity>> getIntelligentRecommendationDisasterList(Object object) {
        return transformer(getApiService().getIntelligentRecommendationDisasterList(DataManager.getInstance().getUser().getToken()));
    }

    /**
     * 请求警情详情
     *
     * @param id
     * @return
     */
    public Observable<List<DisasterInfoEntity>> getDisasterDetail(String id) {
        HashMap<String, String> param = new HashMap<>();
        param.put("zqid", id);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                new Gson().toJson(param));
        return transformer(getApiService().getDisasterDetail(body, DataManager.getInstance().getUser().getToken()));
    }
    /**
     * 请求标绘别表
     *
     * @param zqid
     * @return
     */
    public Observable<List<BiaohuiEntity>> getBiaohuiList(String zqid, String ssbhid) {
        return transformer(getApiService().getBiaohuiList(ssbhid,zqid, DataManager.getInstance().getUser().getToken()));
    }


    /**
     * 修改车辆作战状态
     *
     * @param param
     * @return
     */
    public Observable<SetCLZTBean> changeCarStatus(HashMap<String, Object> param, Boolean ywjq) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                new Gson().toJson(param));
        return transformer(getApiService().changeCarStatus(ywjq ? ApiPath.ChangeCalStatus : ApiPath.ChangeCalStatusWjq, body, DataManager.getInstance().getUser().getToken()));
    }
    /**
     * 修改车辆作战状态
     *
     * @return
     */
    public Observable<CheckLogin> checkUserLogin() {
//        com.juntu.loginlib.util.LoginUtil.getStr(LoginConstant.DEVICE_TYPE,"CZPB")
        return transformer(getApiService().checkUserLogin(com.juntu.loginlib.util.LoginUtil.getStr(LoginConstant.USER_NAME),com.juntu.loginlib.util.LoginUtil.getStr(LoginConstant.DEVICE_TYPE)));
    }
    /**
     * 修改车辆作战状态-无警情
     *
     * @param param
     * @return
     */
//    public Observable<Object> changeCarStatusWjq(HashMap<String, Object> param) {
//        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
//                new Gson().toJson(param));
//        return transformer(getApiService().changeCarStatusWjq(body, DataManager.getInstance().getUser().getToken()));
//    }
    /*
     */
/**
 * 修改车辆作战状态
 *
 * @param param
 * @return
 *//*

    public Observable<Object> changeCarStatus(HashMap<String, Object> param) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                new Gson().toJson(param));
        return getApiService().changeCarStatus(body).compose(FastTransformer.switchSchedulers());
    }
*/

    /**
     * 根据警情ID获取时序化信息
     *
     * @param zqId
     * @return
     */
    public Observable<BaseListEntity<CjXx>> getSxhInfoList(String zqId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("cjid", zqId);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                new Gson().toJson(map));
        return transformer(getApiService().getSxhInfo(body, DataManager.getInstance().getUser().getToken()));
    }

    /**
     * 获取周边的水源
     *
     * @param zqid
     * @param current
     * @return
     */
    public Observable<List<AroundWaterEntity>> getAroundWater(String zqid, int current) {
        current++;
        return transformer(getApiService().getAroundWater(zqid, "ZBZY_XFSY", 10, current, DataManager.getInstance().getUser().getToken()));
    }


    /**
     * 获取警情列表
     *
     * @param zqid
     * @param current
     * @return
     */
    public Observable<BaseListEntity<DisasterListInfoEntity>> getDisasterList(String zqid, int current) {
        current++;
        return transformer(getApiService().getDisasterList(zqid, 10, current, DataManager.getInstance().getUser().getToken()));
    }


    /**
     * @param x
     * @param y
     * @param range
     * @param current
     * @return
     */
    public Observable<BaseListEntity<WaterSourceEntity>> getWaterSource(String zqid,double x, double y, long range, int current, String zblb,Boolean sfcllb) {
        current++;
        return transformer(getApiService().getWaterSourceList(zqid,x, y, range, 20, current,zblb,sfcllb, DataManager.getInstance().getUser().getToken()));
    }
    /**
     * 获取指令列表
     * @param current
     * @return
     */
    public Observable<BaseListEntity<InstructionListEntity>> getInstructionList(String zqid, int current, String key) {
        current++;
        return transformer(getApiService().getInstructionList(zqid,DataManager.getInstance().getUser().getId(), 20, current,key, DataManager.getInstance().getUser().getToken()));
    }
    /**
     * 修改指令状态
     * @return
     */
//    public Observable<Object> uploadInstructionState(HashMap<String, Object> param) {
//        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
//                new Gson().toJson(param));
//        return getApiService().uploadInstructionState(body, DataManager.getInstance().getUser().getToken());
//
//    }
    public Observable<Object> uploadInstructionState(String zlid,String data) {

        return getApiService().uploadInstructionState(zlid,data, DataManager.getInstance().getUser().getToken());

    }

    /**
     * 获取作战班
     *
     * @return
     */
    public Observable<List<CombatTeamEntity>> getCombatTeams() {
        String depId = DataManager.getInstance().getUser().getDept_id();
//        String depId = "333282988351489bb1f07872cf3f3893";
        return transformer(getApiService().getCombatTeams(depId, DataManager.getInstance().getUser().getToken()));
    }


    /**
     * 获取快速反馈模版
     *
     * @return
     */
    public Observable<List<Template>> getTemplates() {
        String depId = DataManager.getInstance().getUser().getDept_id();
//        String depId = "784948f93fbd4030ba58adac923246a5";
        int current = 1;
        int size = 10;
        return transformer(getApiService().getTemplates(depId,
                size, current,
                DataManager.getInstance().getUser().getToken()));
    }


    /**
     * 文件上传
     *
     * @param body
     * @return
     */
    public Observable<List<String>> upLoadFile(RequestBody body) {
        return transformer(getApiService().upLoadFile(body, "/uploader"));
    }


    /**
     * 修改车辆作战状态
     *
     * @param body
     * @return
     */
    public Observable<Object> saveDocument(RequestBody body) {

        return transformer(getApiService().saveDocument(body, DataManager.getInstance().getUser().getToken()));
    }
    /**
     * 占领水源
     *
     * @param body
     * @return
     */
    public Observable<Object> uploadWaterZLSY(RequestBody body) {

        return transformer(getApiService().uploadWaterZLSY(body, DataManager.getInstance().getUser().getToken()));
    }

}
